const express = require('express');
const bodyParser = require('body-parser');
const mssqldb = require('../mssql/mssqlDB')
const expressApp = express();
const port = 3012;
// 接口

expressApp.use(bodyParser.urlencoded({extended: false}));
expressApp.use(bodyParser.json());

expressApp.listen(port,()=>{
    console.log(`接口在${port}端口启动`)
});


// 登录接口
expressApp.post("/user", (req, res) => {

    if (Object.keys(req.body).length === 0) {
        return res.status(200).json({
            success: false,
            msg: "缺少入参"
        });
    }
    if (req.body.accountNum === "") {
        return res.status(200).json({
            success: false,
            msg: "用户名不能为空"
        });
    }
    if (req.body.passWord === "") {
        return res.status(200).json({
            success: false,
            msg: "密码不能为空"
        });
    }
    let QuerySql = `SELECT U.accountNum,INM.nickName,INM.headImage FROM dbo.[User] AS U
LEFT JOIN dbo.Information AS INM ON U.accountNum = INM.accountNum
WHERE U.accountNum = ${req.body.accountNum} AND U.password = ${req.body.passWord} AND U.userState = 0`;
    mssqldb.sql(QuerySql,(err,result)=>{
        if (err) {
            console.log(err)
            return
        }
        if(result.recordset.length === 0){//查询到的数据为[]
            return res.status(200).json({
                success: false,
                msg: "用户名或密码错误！"
            });
        }
        let response = {
            success:true,
            message:'成功',
            data:result.recordsets[0]
        }
        console.log("返回值：：",result)
        return res.status(200).json(response);
    })
})

//获取好友列表
expressApp.post("/Friends",(req,res)=>{

    let QuerySql = `select UserFirend,nickName,headImage,recentChatTime from (select 
(case when UserAcc = ${req.body.accountNum} then FriendAcc 
when FriendAcc = ${req.body.accountNum} then UserAcc else '0' end) as UserFirend,f.recentChatTime
from Friend AS F
inner JOIN DBO.[User] AS U ON F.UserAcc = U.accountNum
where (F.FriendAcc = ${req.body.accountNum} or F.UserAcc = ${req.body.accountNum}) AND F.State = 0) as f inner join Information i on i .accountNum = f.UserFirend`;
    mssqldb.sql(QuerySql,(err,result)=>{
        if (err) {
            console.log(err)
            return
        }
        let response = {
            success:true,
            message:'成功',
            data:result.recordsets[0]
        }
        console.log("返回值：：",result)
        return res.status(200).json(response);

    })
})


//获取群聊列表
expressApp.post("/groups",(req,res)=>{
    console.log("我链接了user接口",req.body.accountNum)
    let QuerySql = `SELECT GC.roomNum AS UserFirend,GC.roomName AS nickName,GC.headImage,GC.limitsPerNum,GC.recentChatTime FROM GroupRelated AS GR
INNER JOIN groupChat AS GC ON GR.RoomID = GC.roomID
WHERE GR.accountNum = ${req.body.accountNum}`
    mssqldb.sql(QuerySql,(err,result)=>{
        if(err){
            console.log(err)
            return
        }
        let response = {
            success:true,
            message:'成功',
            data:result.recordsets[0]
        }
        console.log("群聊返回值：",result)
        return res.status(200).json(response);
    })

})